package dataConnectionPackage;
import java.util.ArrayList;
import java.util.List;



import systemPackage.LocalStrings;
import systemPackage.MainSystem;

import dataTypesPackage.DistanceBetweenLiman;
import dataTypesPackage.Gemi;
import dataTypesPackage.Guzergah;
import dataTypesPackage.Liman;
import dataTypesPackage.LimanCalisani;
import dataTypesPackage.Musteri;
import dataTypesPackage.Satis;
import dataTypesPackage.SatisGorevlisi;
import dataTypesPackage.Sefer;
import dataTypesPackage.SystemAdmin;
import dataTypesPackage.Ulke;


public class DynamicDataSource {

	List<Musteri> musteriListesi = new ArrayList<Musteri>();
	List<Liman> limanListesi = new ArrayList<Liman>();
	List<Ulke> ulkeListesi= new ArrayList<Ulke>();
	List<DistanceBetweenLiman> distanceBetweenLimanListesi = new ArrayList<DistanceBetweenLiman>();
	List<LimanCalisani> limanCalisaniListesi = new ArrayList<LimanCalisani>();
	List<SystemAdmin> systemAdminListesi = new ArrayList<SystemAdmin>();
	List<Gemi> gemiListesi = new ArrayList<Gemi>();
	List<Guzergah> guzergahListesi = new ArrayList<Guzergah>();
	List<Sefer> seferListesi = new ArrayList<Sefer>();
	List<Satis> satisListesi = new ArrayList<Satis>();
	List<SatisGorevlisi> satisGorevlisiListesi = new ArrayList<SatisGorevlisi>();
	
	public String changeLastTransaction(String transactionType, String effectedObjectName){
		MainSystem.changeLastTransaction(transactionType + effectedObjectName);
		return transactionType + effectedObjectName;
	}
	
	// THE PART ABOUT CLIENTS. PUT CLIENT PARTS IN HERE
	public int storeNewMusteri(Musteri musteri) {
		if (musteriListesi.size() == 0)
			musteriListesi.add(new Musteri()); // first element is a null element
		musteri.setMusteriID(musteriListesi.size());
		musteriListesi.add(musteri);
		return musteriListesi.indexOf(musteri);	
	}

	public Musteri loadMusteri(int musteriID) {
		return musteriListesi.get(musteriID);
	}
	
	public boolean editMusteri(Musteri editedMusteri){
		musteriListesi.set(editedMusteri.getMusteriID(), editedMusteri);
		changeLastTransaction(LocalStrings.getLocalString(8), editedMusteri.getName());
		return true;
	}
	
	public List<Musteri> getAllMusteri(){
		return musteriListesi;
	}
	
	public boolean removeMusteri(int MusteriID) {
		// TODO remove musteri is not implemented yet.
		return false;
		
	}
	
	// END OF CLIENT PART
	
	// THE PART ABOUT PORTS. PUT PORTS PARTS IN HERE
	
	public int storeNewLiman(Liman liman, List<DistanceBetweenLiman> distanceBetweenLimans ) {
		if (limanListesi.size() == 0)
			limanListesi.add(new Liman()); // first element is a null element
		liman.setLimanID(limanListesi.size());
		limanListesi.add(liman);
		if (distanceBetweenLimans !=null)
			distanceBetweenLimanListesi.addAll(distanceBetweenLimans);
		changeLastTransaction(LocalStrings.getLocalString(15), liman.getName());
		return limanListesi.indexOf(liman);
	}

	public Liman loadLiman(int limanID) {
		return limanListesi.get(limanID);
	}
	
	public boolean editLiman(Liman editedLiman){
		limanListesi.set(editedLiman.getLimanID(), editedLiman);
		changeLastTransaction(LocalStrings.getLocalString(8), editedLiman.getName());
		return true;
	}
	
	public List<Liman> getAllLiman(){
		return limanListesi;
	}


	
	// END OF PORT PART
	

	// THE PART ABOUT VOYAGES. PUT VOYAGES PARTS IN HERE
	
	public int storeNewSefer(Sefer sefer) {
		// this method stores sefer
		if (seferListesi.size() == 0)
			seferListesi.add(new Sefer()); // first element is a null element
		sefer.setSeferID(seferListesi.size());
		seferListesi.add(sefer);
		changeLastTransaction( LocalStrings.getLocalString(26), sefer.getName());
		return seferListesi.indexOf(sefer);	
	}
	
	// END OF VOYAGES PART
	
	public Ulke getUlkeByName(String ulkeName) {
		// returns the Ulke thats name is "name"
		for (int i=0;i < ulkeListesi.size(); i++){
			if (ulkeName == ulkeListesi.get(i).getName())
				return ulkeListesi.get(i);
		}
		return new Ulke();
	}


	public void populateDatas() {
		// TODO For adding the start values. it should be removed after database connection done
		ulkeListesi.add(new Ulke("Turkey",3));
		ulkeListesi.add(new Ulke("Britain",0));
		ulkeListesi.add(new Ulke("USA",-8));
		limanCalisaniListesi.add(new LimanCalisani(1, "default", "default"));
		systemAdminListesi.add(new SystemAdmin(1, "default", "default"));
		gemiListesi.add(new Gemi());
		guzergahListesi.add(new Guzergah(1,"default"));
		satisGorevlisiListesi.add(new SatisGorevlisi(1, "default"));
		
		
	}


	public Liman getLimanbyID(int limanID) {
		// fetches the according Port.
		
		return limanListesi.get(limanID);
		
	}


	public int getLimanCount() {
		// returns the Limanlist size - 1 as it means the port count.
		return limanListesi.size()-1;
	}


	public List<DistanceBetweenLiman> getAllDistancesBetweenLiman() {
		// returns all distancesBetweenLimanListesi
		return distanceBetweenLimanListesi;
	}


	public List<Gemi> getAllGemi() {
		// returns gemiListesi.
		return gemiListesi;
	}


	public List<Guzergah> getAllGuzergah() {
		// returns guzergahListesi
		return guzergahListesi;
	}


	public List<Sefer> getAllSefer() {
		// returns seferList.
		return seferListesi;
	}


	public int storeNewSatis(Satis satis) {
		// this method stores satis
		if (satisListesi.size() == 0)
			satisListesi.add(new Satis()); // first element is a null element
		satis.setSatisID(satisListesi.size());
		satisListesi.add(satis);
		changeLastTransaction( LocalStrings.getLocalString(47), satis.getSefer().getName());
		return satisListesi.indexOf(satis);	
	}


}
